<?php

class m_proyek extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					m_cabang_id,
                    cabang_nama,
					proyek_kode,
					proyek_nama,
					proyek_pm,
					proyek_nk,
					proyek_nkppn,
					proyek_mulai,
					proyek_kelas,
					proyek_p1,
					proyek_p2,
					proyek_id
				FROM m_proyek
                LEFT JOIN m_cabang
                ON cabang_id = m_cabang_id
				WHERE 
					1 = 1 
		";
        
		if (@$search != ""){
            $sql .= " AND (0 = 1";
            $sql .= " OR m_cabang_id like '%".@$search."%'";
            $sql .= " OR proyek_kode like '%".@$search."%'";
            $sql .= " OR proyek_nama like '%".@$search."%'";
            $sql .= " OR proyek_pm like '%".@$search."%'";
            $sql .= " OR proyek_nk like '%".@$search."%'";
            $sql .= " OR proyek_nkppn like '%".@$search."%'";
            $sql .= " OR proyek_mulai like '%".@$search."%'";
            $sql .= " OR proyek_p1 like '%".@$search."%'";
            $sql .= " OR proyek_p2 like '%".@$search."%'";
            $sql .= ")";
        }
        
		if (@$m_cabang_id != "") $sql .= " AND m_cabang_id like '%".@$m_cabang_id."%'";
		if (@$proyek_kode != "") $sql .= " AND proyek_kode like '%".@$proyek_kode."%'";
		if (@$proyek_nama != "") $sql .= " AND proyek_nama like '%".@$proyek_nama."%'";
		if (@$proyek_pm != "") $sql .= " AND proyek_pm like '%".@$proyek_pm."%'";
		if (@$proyek_nk != "") $sql .= " AND proyek_nk like '%".@$proyek_nk."%'";
		if (@$proyek_nkppn != "") $sql .= " AND proyek_nkppn like '%".@$proyek_nkppn."%'";
		if (@$proyek_mulai != "") $sql .= " AND proyek_mulai like '%".@$proyek_mulai."%'";
		if (@$proyek_p1 != "") $sql .= " AND proyek_p1 like '%".@$proyek_p1."%'";
		if (@$proyek_p2 != "") $sql .= " AND proyek_p2 like '%".@$proyek_p2."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows)." , ".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('m_proyek',$data);
		return 1;
	}
	
	function _update($data,$id){
		$this->db->where('proyek_id',$id);
		$this->db->update('m_proyek',$data);
		return 1;
	}
	
	function _delete($id){
		$this->db->where('proyek_id',$id);
		$this->db->delete('m_proyek');
		return 1;
	}
	
	function _deactivate($data,$id){
		$this->db->where('proyek_id',$id);
		$this->db->update('m_proyek',$data);
		return 1;
	}
    
	function _kelas(){
		$sql = "SELECT DISTINCT proyek_kelas proyek_kelas FROM m_proyek";
		$query = $this->db->query($sql);
        $result = $query->result();
        $array = array();
        foreach($result as $row){
            if ($row->proyek_kelas != null)
                $array[] = $row->proyek_kelas;
        }
		return $array;
	}
}